package xyz.xiangyushui.dao.user.member;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import xyz.xiangyushui.dao.Dao;
import xyz.xiangyushui.javaBean.membership.Reservation;

public class ReservationHistoryDao extends Dao {
	public void addResevationRecord(String userName, String date_storeId,
			String commodityIdName, int num, long time) {
		db.executeUpdate(
				"insert into reservationHistory(userName,date_storeId,commodityIdName,quantity,time) values(?,?,?,?,?)  ",
				new Object[] { userName, date_storeId, commodityIdName, num,
						time });
	}

	public List<Reservation> getMyChangableReservation(String userName) {
		List<Reservation> reservations = new LinkedList<>();
		List<Object> list = db.excuteQuery(
				"select * from reservationHistory where userName=? ",
				new Object[] { userName });
		System.out.println(list.size());
		for (Object o : list) {
			@SuppressWarnings("unchecked")
			Map<String, Object> m = (Map<String, Object>) o;
			String date_storeId = (String) m.get("date_storeId");
			String[] date_storeIdParts = date_storeId.split("_");
			long salesDay = Long.parseLong(date_storeIdParts[0]);
			if (new Date().getTime() >= salesDay) {
				// System.out.println(new Date().before(new Date(salesDay)));
				// System.out.println(new
				// SimpleDateFormat("yyyy-MM-dd").format(new Date()));
				// System.out.println(new
				// SimpleDateFormat("yyyy-mm-dd").format(new Date(salesDay)));
				// System.out.println("bigger");
				continue;
			} else {
				Reservation r = new Reservation();
				r.setId((int) m.get("id"));
				r.setDate_storeId(date_storeId);
				r.setUserName(userName);
				r.setCommodityIdName((String) m.get("commodityIdName"));
				r.setQuantity((int) m.get("quantity"));
				r.setTime((long) m.get("time"));
				reservations.add(r);
			}

		}

		return reservations;
	}

	public void deleteReservation(int id) {
		db.executeUpdate("delete from reservationHistory where id=?",
				new Object[] { id });
	}

	public Reservation getReservation(int id) {
		List<Object> list = db.excuteQuery(
				"select * from reservationHistory where id=? ",
				new Object[] { id });
		if (list.isEmpty()) {
			return null;
		}
		@SuppressWarnings("unchecked")
		Map<String, Object> m = (Map<String, Object>) list.get(0);
		Reservation r = new Reservation();
		r.setId((int) m.get("id"));
		r.setDate_storeId((String) m.get("date_storeId"));
		r.setUserName((String) m.get("userName"));
		r.setCommodityIdName((String) m.get("commodityIdName"));
		r.setQuantity((int) m.get("quantity"));
		r.setTime((long) m.get("time"));
		return r;
	}

}
